package com.example.studentmanagersystem;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.fs.adapters.TecUpdateGradeAdapter;
import com.fs.po.StuSocre;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 老师为学生打分界面,学生和成绩将会显示在ListView中，当点击ListView列表项时会弹出自定义对话框，对成绩进行修改
 * 摘要：自定义ListView、自定义对话框
 * @author chenwenfei
 *
 */
public class TeacherSetGradeActivity extends Activity {
	
	private ListView infoListView;
	private Button actionBack,actionMenu,frashButton;
	private TecUpdateGradeAdapter sroceAdapter;
	
	String name,password;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.teacher_showgrade_activity);
		
		this.getLoginInfo();
		
		this.init();
		
		this.setAdapter();
		
		this.setListener();
	}
	
	/**
	 * 获取用户登录信息
	 */
	private void getLoginInfo() {
		 this.name=MainActivity.username;
		 this.password=MainActivity.password;
	}
	
	/**
	 * 初始化控件
	 */
	private void init() {
		this.infoListView=(ListView) this.findViewById(R.id.teacher_showgrade_stuListView);
		this.actionBack=(Button) this.findViewById(R.id.tecSetGrade_action_backButton);
		this.actionMenu=(Button) this.findViewById(R.id.tecSetGrade_action_menu);
		this.frashButton=(Button) this.findViewById(R.id.teacher_showgrade_activity_frash);
	}
	
	
	
	/**
	 * setAdapter为列表设置监听器
	 */
	private void setAdapter() {
		this.sroceAdapter=new TecUpdateGradeAdapter(this);
		this.infoListView.setAdapter(sroceAdapter);
		
	}
	
	/**
	 * 设置数据，调用获取数的方法
	 */
	private void setData() {
		 this.getData("tec_get_stu_grade");
		 
	}
	
	
	
	/**
	 * 设置监听器
	 */
	
	private void setListener() {
		
		//导航返回监听
		this.actionBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TeacherSetGradeActivity.this.finish();
			}
		});
		
		//导航菜单监听
		this.actionMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v, TeacherSetGradeActivity.this);
				popupMenuUtil.actionBackListener();
			}
		});
		
		//刷新监听,当点击刷新按钮时，就会从数据库获取相关信息
		this.frashButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				setData();//设置数据
//				sroceAdapter.notifyDataSetChanged();//刷新数据
			}
		});
		
		//列表点击监听,长按事件，长按列表项就会弹出对话框，对话框中就可以显示对学生的成绩进行修改
		this.infoListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
				 
				
				return false;
			}
		});
		
		
		
	}
	
	
	
	
	
	 List<StuSocre> scorelist;
	/**
	 * 设置listView的显示数据,从服务器中获取
	 */
	private void getData(String action) {
		
		this.scorelist = new ArrayList<StuSocre>();
		
			
			RequestParams params = new RequestParams();
			params.addBodyParameter("action", action);
			params.addBodyParameter("tec_id", ""+MainActivity._id);
			
			
			HttpUtils httpUtils=new HttpUtils();
			httpUtils.send(HttpMethod.POST, URL.SOCRE_URL, params, new RequestCallBack<String>() {

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {

					String jsonString=responseInfo.result;
					setStuData(jsonString);
				}

				@Override
				public void onFailure(HttpException error, String msg) {
					
				}

			});
		
	}
	
	/**
	 * 对数据进行解析
	 * @param jsonString
	 * @return
	 */
	public void setStuData(String jsonString) {
		
		StuSocre scorestu=null;
		System.out.println(jsonString);
		if(jsonString!=null){
			System.out.println(jsonString);
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				JSONArray array=jsonObject.getJSONArray("stu_score");
				
				
				for(int i=0;i<array.length();i++){
					JSONObject json=array.getJSONObject(i);
					
					
					String name=json.getString("stu_name");
					int score=Integer.parseInt(json.getString("score"));
					int stu_id=Integer.parseInt(json.getString("stu_id"));
					int tec_id=Integer.parseInt(json.getString("tec_id"));
					int class_id=Integer.parseInt(json.getString("class_id"));
					
					
					scorestu=new StuSocre(name, score,stu_id,tec_id,class_id);
					this.scorelist.add(scorestu);
				}
				
				Toast.makeText(TeacherSetGradeActivity.this, ""+scorestu, 0).show();
				//为适配器设置数据
				this.sroceAdapter.setAdapterData(scorelist,handler);
				this.sroceAdapter.notifyDataSetChanged();
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
		}
	}
	

	/**
	 * 更新UI界面
	 */
	Handler handler=new Handler(){
		
		public void handleMessage(Message msg) {
			
			if(msg.what==1){
				
				getData("tec_get_stu_grade");
				
			}
			
		}
		
	};
	
	
	
	

}
